/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.customize.enums.AbnormalEnum;
import com.pig4cloud.pigx.common.customize.exception.DuckweedException;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.user.dto.ImputationRecordPageDTO;
import com.pig4cloud.pigx.user.entity.ImputationRecord;
import com.pig4cloud.pigx.user.enums.ImputationRecordEnum;
import com.pig4cloud.pigx.user.service.ImputationRecordService;
import com.pig4cloud.pigx.user.vo.AdminPageDemoVO;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 归集列表
 *
 * @author pigx code generator
 * @date 2022-11-22 14:55:33
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/imputation_record" )
@Api(value = "imputation_record", tags = "归集列表管理")
public class ImputationRecordController {

    private final  ImputationRecordService imputationRecordService;

    /**
     * 分页查询
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping("/page" )
    public R<AdminPageDemoVO<ImputationRecord>> getImputationRecordPage(@RequestBody ImputationRecordPageDTO dto) {
		try {
			QueryWrapper<ImputationRecord> wrapper = new QueryWrapper<ImputationRecord>() {{
				if (dto.getUserId() != null) {
					like("user_id", dto.getUserId());
				}
				if (dto.getStatus() != null && !dto.getStatus().isEmpty()) {
					eq("state", dto.getStatus());
				}
				if (dto.getChain() != null && !dto.getChain().isEmpty()) {
					eq("chain", dto.getChain());
				}
				if (dto.getStartTime() != null) {
					ge("create_time", dto.getStartTime());
				}
				if (dto.getEndTime() != null) {
					le("create_time", dto.getEndTime());
				}
				orderByDesc("create_time");
			}};
			Page<ImputationRecord> page = imputationRecordService.page(new Page<ImputationRecord>() {{
				setCurrent(dto.getCurrent());
				setSize(dto.getSize());
			}},wrapper);
			for (ImputationRecord record : page.getRecords()) {
				record.setState(ImputationRecordEnum.Status.findName(record.getState()));
			}
			ImputationRecord one = imputationRecordService.getOne(wrapper.select("sum(quantity) 'quantity'"));
			return R.ok(new AdminPageDemoVO<ImputationRecord>(){{
				setPage(page);
				setStatistics(one);
			}});
		} catch (Exception e) {
			throw new DuckweedException(AbnormalEnum.MSG.DISCONNECTED);
		}
    }


    /**
     * 通过id查询归集列表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('user_imputationrecord_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(imputationRecordService.getById(id));
    }


	/**
	 * 创建归集
	 * @param imputationRecord 请求参数
	 */
	@PostMapping
	public R create(@RequestBody ImputationRecord imputationRecord){
		imputationRecordService.create(imputationRecord);
		return R.ok();
	}

    /**
     * 修改归集列表
     * @param imputationRecord 归集列表
     * @return R
     */
    @ApiOperation(value = "修改归集列表", notes = "修改归集列表")
    @SysLog("修改归集列表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('user_imputationrecord_edit')" )
    public R updateById(@RequestBody ImputationRecord imputationRecord) {
        return R.ok(imputationRecordService.updateById(imputationRecord));
    }

    /**
     * 通过id删除归集列表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除归集列表", notes = "通过id删除归集列表")
    @SysLog("通过id删除归集列表" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('user_imputationrecord_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(imputationRecordService.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param imputationRecord 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('user_imputationrecord_export')" )
    public List<ImputationRecord> export(ImputationRecord imputationRecord) {
        return imputationRecordService.list(Wrappers.query(imputationRecord));
    }
}
